package com.shaco.heroproject.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.shaco.heroproject.R;
import com.shaco.heroproject.adapter.AbilityAdapter;
import com.shaco.heroproject.entity.AbilityResult;
import com.shaco.heroproject.entity.Result_ability;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/5.
 */
public class AbilityActivity extends Activity implements View.OnClickListener {
    private Button ability_findBtn, activity_back_story_backBtn;
    private Spinner abilitySp1, abilitySp2;
    private AbilityAdapter adapter;
    private List<Result_ability> results;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ability);

        initView();
    }

    private void initView() {
        abilitySp1 = (Spinner) findViewById(R.id.abilitySp1);
        abilitySp2 = (Spinner) findViewById(R.id.abilitySp2);
        ability_findBtn = (Button) findViewById(R.id.ability_findBtn);
        ability_findBtn.setOnClickListener(this);
        activity_back_story_backBtn = (Button) findViewById(R.id.activity_back_story_backBtn);
        activity_back_story_backBtn.setOnClickListener(this);


        RecyclerView recyclerView = (RecyclerView) findViewById(
                R.id.abilityRecycleView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(
                this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new AbilityAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ability_findBtn:
                initData();
                break;
            case R.id.activity_back_story_backBtn:
                finish();
                break;
        }

    }

    private void initData() {
        String infor1 = abilitySp1.getSelectedItem().toString();
        String infor2 = abilitySp2.getSelectedItem().toString();
        //获取spinner显示的字符串,替换成对应的url类型段
        infor1 = infor1.replace("符印", "seal");
        infor1 = infor1.replace("印记", "mark");
        infor1 = infor1.replace("雕文", "glyph");
        infor1 = infor1.replace("精华", "quintessence");
        //获取spinner显示的字符串,替换成对应的url等级段
        infor2 = infor2.replace("一级", "1");
        infor2 = infor2.replace("二级", "2");
        infor2 = infor2.replace("三级", "3");
        String url = "http://lol.data.shiwan.com/lolRune/?tag=" + infor1 + "&tier=" + infor2;
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // TODO Auto-generated method stub
                        Gson gson = new Gson();
                        AbilityResult ar = gson.fromJson(response,
                                AbilityResult.class);
                        if (ar != null) {
                            results = ar.getResult();
                            adapter.addData(results);
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                error.printStackTrace();

            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.add(request);

    }
}